/*
 *接口编写处...
*如果接口需要做Action的权限验证，请在Action上使用属性
*如: [ApiActionPermission("warehousemodel",Enums.ActionPermissionOptions.Search)]
 */
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using VOL.Entity.DomainModels;
using VOL.demo.IServices;
using VOL.Core.Filters;
using VOL.Core.Enums;
using VOL.Core.Utilities;
using System.Linq;
using Microsoft.AspNetCore.Authorization;

namespace VOL.demo.Controllers
{
    public partial class warehousemodelController
    {
        private readonly IwarehousemodelService _service;//访问业务代码
        private readonly IHttpContextAccessor _httpContextAccessor;

        [ActivatorUtilitiesConstructor]
        public warehousemodelController(
            IwarehousemodelService service,
            IHttpContextAccessor httpContextAccessor
        )
        : base(service)
        {
            _service = service;
            _httpContextAccessor = httpContextAccessor;
        }

        /// <summary>
        /// 根据仓库ID获取仓库信息
        /// </summary>
        /// <param name="warehouseId">仓库ID</param>
        /// <returns>仓库信息</returns>
        [HttpGet("GetWarehouseById/{warehouseId}")]
        [AllowAnonymous]  // 允许匿名访问，无需JWT验证
        public async Task<ActionResult<WebResponseContent>> GetWarehouseById(long warehouseId)
        {
            if (warehouseId <= 0)
            {
                return Ok(WebResponseContent.Instance.Error("仓库ID不能为空或小于等于0"));
            }

            try
            {
                var warehouse = await _service.GetWarehouseByIdAsync(warehouseId);
                if (warehouse == null)
                {
                    return Ok(WebResponseContent.Instance.Error("未找到对应的仓库信息"));
                }

                return Ok(WebResponseContent.Instance.OK("获取仓库信息成功", warehouse));
            }
            catch (Exception ex)
            {
                return Ok(WebResponseContent.Instance.Error($"获取仓库信息失败：{ex.Message}"));
            }
        }

        /// <summary>
        /// 获取所有启用状态的仓库列表
        /// </summary>
        /// <returns>仓库列表</returns>
        [HttpGet("GetActiveWarehouses")]
        [AllowAnonymous]  // 允许匿名访问，无需JWT验证
        public async Task<ActionResult<WebResponseContent>> GetActiveWarehouses()
        {
            try
            {
                var warehouses = await _service.GetActiveWarehousesAsync();
                
                // 转换为前端下拉选项格式
                var warehouseOptions = warehouses.Select(w => new
                {
                    value = w.Id,
                    label = w.WarehouseName,
                    code = w.WarehouseCode,
                    address = w.Warehouseaddr,
                    capacity = w.Capacity,
                    status = w.status,
                    desc = w.WarehouseDesc
                }).ToList();

                return Ok(WebResponseContent.Instance.OK("获取仓库列表成功", warehouseOptions));
            }
            catch (Exception ex)
            {
                return Ok(WebResponseContent.Instance.Error($"获取仓库列表失败：{ex.Message}"));
            }
        }

        /// <summary>
        /// 根据仓库编号获取仓库信息
        /// </summary>
        /// <param name="warehouseCode">仓库编号</param>
        /// <returns>仓库信息</returns>
        [HttpGet("GetWarehouseByCode")]
        [AllowAnonymous]  // 允许匿名访问，无需JWT验证
        public async Task<ActionResult<WebResponseContent>> GetWarehouseByCode(string warehouseCode)
        {
            if (string.IsNullOrEmpty(warehouseCode))
            {
                return Ok(WebResponseContent.Instance.Error("仓库编号不能为空"));
            }

            try
            {
                var warehouse = await _service.GetWarehouseByCodeAsync(warehouseCode);
                if (warehouse == null)
                {
                    return Ok(WebResponseContent.Instance.Error("未找到对应的仓库信息"));
                }

                return Ok(WebResponseContent.Instance.OK("获取仓库信息成功", warehouse));
            }
            catch (Exception ex)
            {
                return Ok(WebResponseContent.Instance.Error($"获取仓库信息失败：{ex.Message}"));
            }
        }

        /// <summary>
        /// 获取仓库下拉选项（简化版，用于表单选择）
        /// </summary>
        /// <returns>仓库选项列表</returns>
        [HttpGet("GetWarehouseOptions")]
        [AllowAnonymous]  // 允许匿名访问，无需JWT验证
        public async Task<ActionResult<WebResponseContent>> GetWarehouseOptions()
        {
            try
            {
                var warehouses = await _service.GetActiveWarehousesAsync();
                
                // 转换为简化的下拉选项格式
                var options = warehouses.Select(w => new
                {
                    value = w.Id,
                    label = w.WarehouseName,
                    code = w.WarehouseCode
                }).ToList();

                return Ok(WebResponseContent.Instance.OK("获取仓库选项成功", options));
            }
            catch (Exception ex)
            {
                return Ok(WebResponseContent.Instance.Error($"获取仓库选项失败：{ex.Message}"));
            }
        }
    }
}
